package mobileann.mafamily.utils;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mobileann.mafamily.entity.ActionEntity;
import mobileann.mafamily.entity.ActionEntity3;
import android.content.Context;

import com.mofind.android.base.L;
import com.mofind.java.utils.TimeUtils;

public class URLUtils {
	public static final String URL_CLIENT="http://client.mobileann.com/";
	public static final String URL_API="http://api.mobileann.com/";
	//	public static final String URL_CLIENT="http://client.mobileann.cn/";
	//	public static final String URL_API="http://api.mobileann.cn/";
	//    public static final String URL_CLIENT="http://mobileann.xicp.net:7088/";
	//    public static final String URL_API="http://mobileann.xicp.net:7088/";//测试
	// 注册
	public static final String URL_REGISTER = URL_CLIENT+"api/gpc_device.php";

	// 上传统计数据
	public static final String URL_ACTION = URL_CLIENT+"api/gpc_activity.php";

	// app升级
	public static final String URL_APK_UPDATE = URL_CLIENT+"api/gpc_update.php";

	// 反馈
	public static final String URL_FEEDBACK = URL_CLIENT+"api/gpc_feedback.php";

	// 帮助
	public static final String URL_HELPER = URL_CLIENT+"page/gpc_help.php";
	/** 安卓(定位及应用详情) */
	public static final String URL_HELPER_1 = "http://client.mobileann.com/page/gpc_help.php?os=android&open=1";
	/**安卓(离线)*/
	public static final String URL_HELPER_2 = "http://client.mobileann.com/page/gpc_help.php?os=android&open=2";
	/** 安卓(家庭成员等问题) */
	public static final String URL_HELPER_3 = "http://client.mobileann.com/page/gpc_help.php?os=android&open=3";
	public static final String URL_HELPER_NEW = "http://client.mobileann.com/page/gpc_help.php?os=android";

	//TODO 成长体验
	public static final String URL_GROWUP = URL_CLIENT+"page/gpc_help.php";

	// 最新活动
	public static final String URL_NEW_ACTIVITY = URL_CLIENT+"api/gpc_get_promotion.php";

	// 用户指南
	public static final String URL_USER_GUIDE = URL_CLIENT+"page/gpc_manual.php";

	// 免责协议
	public static final String URL_DISCLAIMER = URL_CLIENT+"page/gpc_protocol.php";

	// 在线统计接口
	public static final String URL_ONLINE = URL_CLIENT+"api/gpc_online.php";
	// 成长体系上传数据接口
	public static final String URL_GROWTH_UPLOAD = URL_API+"api/gpc_set_api.php";
	public static final String URL_TASK_GET = URL_API+"/api/gpc_get_api.phps?";// query_string=uid+_+irad+_+"IntegralDisposable"
	// 下载路径
	public static String downloadPath = null;

	// 最新版本
	public static String updateVersionName = null;

	public static Map<String, String> getActionPara(Context c,
			List<ActionEntity> datas) {
		Map<String, String> map = new HashMap<String, String>();
		//	map.put("uid", SPUtils.getUidByPhoneNum());
		map.put("uid", SPUtils.getMySelf(SPUtils.MYUID));
		map.put("imei", PhoneInfoUtils.getIMEI(c));
		map.put("imsi", PhoneInfoUtils.getIMSI(c));
		map.put("uuid", PhoneInfoUtils.getMyUUID(c));
		//	map.put("key", getKey(c, SPUtils.getUidByPhoneNum()));
		map.put("key", getKey(c, SPUtils.getMySelf(SPUtils.MYUID)));
		//	map.put("date", getCurrentDateString());
		map.put("data", getJsonData(datas));
		return map;
	}

	public static Map<String, String> getActionClickPara(Context c,
			List<ActionEntity3> datas) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("uid", SPUtils.getMySelf(SPUtils.MYUID));
		map.put("imei", PhoneInfoUtils.getIMEI(c));
		map.put("imsi", PhoneInfoUtils.getIMSI(c));
		map.put("uuid", PhoneInfoUtils.getMyUUID(c));
		//	map.put("key", getKey(c, SPUtils.getUidByPhoneNum()));
		map.put("key", getKey(c,SPUtils.getMySelf(SPUtils.MYUID)));
		//	map.put("date", getCurrentDateString());
		map.put("data", getClickJsonData(datas));
		return map;
	}

	public static String getClickJsonData(List<ActionEntity3> datas) {
		String str = "[";
		//
		if (!datas.isEmpty()) {
			for (ActionEntity3 a : datas) {
				str += (a.toString() + ",");
			}
		}
		//
		str = str.substring(0, str.length() - 1);
		str += "]";
		str = "[{\"" + getCurrentDateString() + "\":" + str + "}]";

		L.d("str= " + str);
		return str;
	}

	public static String getJsonData(List<ActionEntity> datas) {
		String str = "[";
		//
		if (!datas.isEmpty()) {
			for (ActionEntity a : datas) {
				str += (a.toString() + ",");
			}
		}
		//
		str = str.substring(0, str.length() - 1);
		str += "]";
		str = "[{\"" + getCurrentDateString() + "\":" + str + "}]";

		L.d("str= " + str);
		return str;
	}

	public static String getCurrentDateString() {
		Calendar c = Calendar.getInstance();
		return c.get(Calendar.YEAR)
				+ TimeUtils.timeFormat(c.get(Calendar.MONTH) + 1)
				+ TimeUtils.timeFormat(c.get(Calendar.DAY_OF_MONTH));
	}

	public static Map<String, String> getRegisterPara(Context c, String uid) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("uid", uid);
		map.put("imei", PhoneInfoUtils.getIMEI(c));
		map.put("imsi", PhoneInfoUtils.getIMSI(c));
		map.put("uuid", PhoneInfoUtils.getMyUUID(c));
		map.put("key", getKey(c, uid));
		map.put("sim_num", PhoneInfoUtils.getPhoneNumber(c));
		map.put("os_name", "android");
		map.put("os_version", PhoneInfoUtils.getRelease());
		map.put("cs_version", PhoneInfoUtils.getVersionCode(c));
		map.put("cs_channel", PhoneInfoUtils.getVersionChannel(c));
		map.put("device", PhoneInfoUtils.getPhoneModel());
		return map;
	}

	public static Map<String, String> getOnlinePara(Context c, String uid) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("uid", uid);
		map.put("imei", PhoneInfoUtils.getIMEI(c));
		map.put("imsi", PhoneInfoUtils.getIMSI(c));
		map.put("uuid", PhoneInfoUtils.getMyUUID(c));
		map.put("key", getKey(c, uid));
		map.put("role",
				(("0".equals(SPUtils.getUID()) ? "" : (SPUtils.getRole() + ""))));
		map.put("os_name", "android");
		map.put("cs_version", PhoneInfoUtils.getVersionCode(c));
		map.put("cs_channel", PhoneInfoUtils.getVersionChannel(c));
		return map;
	}

	public static Map<String, String> getApkUpdatePara(Context c, String uid) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("uid", uid);
		map.put("imei", PhoneInfoUtils.getIMEI(c));
		map.put("imsi", PhoneInfoUtils.getIMSI(c));
		map.put("uuid", PhoneInfoUtils.getMyUUID(c));
		map.put("key", getKey(c, uid));
		map.put("os_name", "android");
		map.put("cs_version", PhoneInfoUtils.getVersionCode(c));
		L.i("当前版本号= " + map.get("cs_version"));
		return map;
	}

	public static Map<String, String> getDeedBackPara(Context c, String uid,
			String telephone, String content) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("uid", uid);
		map.put("imei", PhoneInfoUtils.getIMEI(c));
		map.put("imsi", PhoneInfoUtils.getIMSI(c));
		map.put("uuid", PhoneInfoUtils.getMyUUID(c));
		map.put("key", getKey(c, uid));
		map.put("telephone", telephone);
		map.put("content", content);
		return map;
	}

	public static String getKey(Context c, String uid) {
		String uuid = PhoneInfoUtils.getMyUUID(c);
		String imei = PhoneInfoUtils.getIMEI(c);
		String imsi = PhoneInfoUtils.getIMSI(c);
		String k = uuid + "mo" + imei + "family" + imsi + "an" + uid;
		return MD5.getMD5(k.getBytes());
	}

	public static Map<String, String> getMobileannActivityPara(Context c,
			String uid) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("uid", uid);
		map.put("imei", PhoneInfoUtils.getIMEI(c));
		map.put("imsi", PhoneInfoUtils.getIMSI(c));
		map.put("uuid", PhoneInfoUtils.getMyUUID(c));
		map.put("key", getKey(c, uid));
		//
		map.put("channel", PhoneInfoUtils.getVersionChannel(c));
		return map;
	}

	/*
	 * 获取要上传的参数（上传积分体系）
	 */
	public static Map<String, String> getIntegralPara(Context c, String uid) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("from", "integral");
		map.put("uid", uid);
		map.put("data", MyTaskUtils.getInstance().getJsonDatas(c, uid));
		return map;
	}

	public static Map<String, String> getAwardPara(Context c, String uid,
			String code, int codeTag) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("from", "integral_get_award");
		map.put("uid", uid);
		map.put("data",
				// MyTaskUtils.getInstance().getAward3DES(c, SPUtils.getUID2(),
				// code, codeTag));
				MyTaskUtils.getInstance().getAward3DES(c,
						SPUtils.getMySelf(SPUtils.MYUID), code, codeTag));
		return map;
	}
}
